1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.eventbus.outside;
18  
19  import com.google.common.eventbus.EventBus;
20  import com.google.common.eventbus.Subscribe;
21  
22  import junit.framework.TestCase;
23  
24  import java.util.concurrent.atomic.AtomicInteger;
25  import java.util.concurrent.atomic.AtomicReference;
26  
27  /**
28   * Test cases for {@code EventBus} that must not be in the same package.
29   *
30   * @author Louis Wasserman
31   */
32  public class OutsideEventBusTest extends TestCase {
33  
34    /*
35     * If you do this test from common.eventbus.EventBusTest, it doesn't actually test the behavior.
36     * That is, even if exactly the same method works from inside the common.eventbus package tests,
37     * it can fail here.
38     */
39    public void testAnonymous() {
40      final AtomicReference<String> holder = new AtomicReference<String>();
41      final AtomicInteger deliveries = new AtomicInteger();
42      EventBus bus = new EventBus();
43      bus.register(new Object() {
44        @Subscribe
45        public void accept(String str) {
46          holder.set(str);
47          deliveries.incrementAndGet();
48        }
49      });
50  
51      String EVENT = "Hello!";
52      bus.post(EVENT);
53  
54      assertEquals("Only one event should be delivered.", 1, deliveries.get());
55      assertEquals("Correct string should be delivered.", EVENT, holder.get());
56    }
57  }